package algotithm.leetcode.listnode.test143;

import algotithm.leetcode.listnode.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/17-18:40
 * @URL https://leetcode-cn.com/problems/reorder-list/submissions/
 * @Title 143. 重排链表
 */
public class Solution {

    public static void main(String[] args) {

    }

    public void reorderList(ListNode head) {
        if (head == null) {
            return;
        }
        List<ListNode> list = new ArrayList<>();
        while (head != null) {
            list.add(head);
            head = head.next;
        }
        int i = 0 , j = list.size() - 1;
        while (i < j) {
            list.get(i).next = list.get(j);
            i++;
            if (i >= j) {
                break;
            }
            list.get(j).next = list.get(i);
            j--;
        }
        list.get(i).next = null;
    }

}
